package com.cyt.work.wb.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.work.wb.dao.WbBrandTypeDao;
import com.cyt.work.wb.model.po.WbBrandType;
import com.cyt.work.wb.model.vo.WbBrandTypeVo;
import com.cyt.work.wb.service.WbBrandTypeService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 维保模块-品牌分类表Service层实现类
 *
 * @author shengQiang Yu
 * @date 2019-11-28 9:25:27
 */
@Service("wbBrandTypeService")
public class WbBrandTypeServiceImpl extends BaseServiceImpl<WbBrandType> implements WbBrandTypeService {

    @Resource
    private WbBrandTypeDao wbBrandTypeDao;

    @Autowired
    public void setBaseDao(WbBrandTypeDao wbBrandTypeDao) {
        this.baseDao = wbBrandTypeDao;
    }

    /**
     * 查询对象集合
     *
     * @param wbBrandTypeVo 对象
     * @return List 对象集合
     * @author shengQiang Yu
     * @date 2019-11-28 9:25:27
     */
    @Override
    public List<WbBrandType> listExample(WbBrandTypeVo wbBrandTypeVo) {
        Example example = new Example(WbBrandType.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return wbBrandTypeDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param wbBrandTypeVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:25:27
     */
    @Override
    public ReturnResult page(WbBrandTypeVo wbBrandTypeVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WbBrandType> list = listExample(wbBrandTypeVo);
        PageInfo<WbBrandType> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wbBrandTypeVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:25:27
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WbBrandTypeVo wbBrandTypeVo, SysUser sysUser) {
        if (StringUtils.isBlank(wbBrandTypeVo.getId())) {
            WbBrandType wbBrandType = new WbBrandType(sysUser);
            wbBrandType.setApplyId(wbBrandTypeVo.getApplyId());
            wbBrandType.setName(wbBrandTypeVo.getName());
            wbBrandType.setRemarks(wbBrandTypeVo.getRemarks());
            wbBrandType.setParentId(wbBrandTypeVo.getParentId());
            wbBrandType.setParentIds(wbBrandTypeVo.getParentIds());
            wbBrandType.setSort(Integer.parseInt(wbBrandTypeVo.getSort()));
            wbBrandTypeDao.insert(wbBrandType);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WbBrandType wbBrandType = wbBrandTypeDao.selectByPrimaryKey(wbBrandTypeVo.getId());
            wbBrandType.setApplyId(wbBrandTypeVo.getApplyId());
            wbBrandType.setName(wbBrandTypeVo.getName());
            wbBrandType.setRemarks(wbBrandTypeVo.getRemarks());
            wbBrandType.setParentId(wbBrandTypeVo.getParentId());
            wbBrandType.setParentIds(wbBrandTypeVo.getParentIds());
            wbBrandType.setSort(Integer.parseInt(wbBrandTypeVo.getSort()));
            wbBrandType.setUpdateTime(new Date());
            wbBrandType.setUpdateUser(sysUser.getId());
            wbBrandTypeDao.updateByPrimaryKey(wbBrandType);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wbBrandTypeVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:25:27
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WbBrandTypeVo wbBrandTypeVo, SysUser sysUser) {
        //要修改的字段
        WbBrandType wbBrandType = new WbBrandType();
        wbBrandType.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wbBrandType.setUpdateUser(sysUser.getId());
        wbBrandType.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WbBrandType.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbBrandTypeVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbBrandTypeVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        wbBrandTypeDao.updateByExampleSelective(wbBrandType, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wbBrandTypeVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:25:27
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WbBrandTypeVo wbBrandTypeVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WbBrandType.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbBrandTypeVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbBrandTypeVo.getId().split(",")));
        }
        wbBrandTypeDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}